<%@ page import="com.kaikeba.bean.Student" %>
<%@ page import="com.kaikeba.service.StudentService" %>
<%@ page import="java.util.List" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/2/18
  Time: 21:32
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
  <head>
    <title></title>
    <style type="text/css">
      #head{
        text-align: center;
        background-color: cornflowerblue;
        height: 150px;
        line-height: 150px;
      }
      #body{
        text-align: center;
      }
      table{
        width: 800px;
        margin: 30px auto;
        border-collapse: collapse;
      }
      td{
        border: 1px solid black;
        text-align: center;
      }
      .stu{
        width: 65px;
        text-align: center;
      }
      #stu{
        width: 65px;
        text-align: center;
      }
    </style>
    <%
      StudentService service = new StudentService();
      List<Student> list = service.get().selectAll();
      request.setAttribute("all",list);
    %>
  </head>
  <body>
  <div id="head">
    <h1>学生信息管理</h1>
  </div>
  <div id="body">

    <table>
      <tr>
        <td>学生Id</td><td>学生学号</td><td>学生姓名</td><td>学生班级</td><td>学生年龄</td><td colspan="2">功能操作</td>
      </tr>
      <c:forEach items="${requestScope.all}" var="stu">
        <form method="post" class="form">
          <tr style="height: 40px">
            <td><input type="text" id="stu" name="stuId" value="${stu.studentId}" readonly="readonly"></td>
            <td><input type="text" class="stu" name="stuNumber" value="${stu.studentNumber}" readonly="readonly"></td>
            <td><input type="text" class="stu" name="stuName" value="${stu.studentName}" readonly="readonly"></td>
            <td><input type="text" class="stu" name="stuClass" value="${stu.studentClass}" readonly="readonly"></td>
            <td><input type="text" class="stu" name="stuAge" value="${stu.studentAge}" readonly="readonly"></td>
            <td><input type="button" class="update" value="修改"/></td>
            <td><input type="submit" class="delete" name="" value="删除"/></td>
          </tr>
        </form>
      </c:forEach>
      <form method="post" action="/insert">
        <tr style="height: 40px">
            <td><input type="text" class="stu" name="stuId" readonly="readonly" value="无需添加"></td>
            <td><input type="text" class="stu" name="stuNumber"></td>
            <td><input type="text" class="stu" name="stuName" ></td>
            <td><input type="text" class="stu" name="stuClass"></td>
            <td><input type="text" class="stu" name="stuAge"></td>
            <td colspan="2"><input type="submit" id="insert" value="添加"/></td>
        </tr>
      </form>
    </table>
  </div>
  </body>
  <script src="jquery-3.5.0.min.js" type="text/javascript" charset="utf-8"></script>
  <script type="text/javascript">
    $(function () {
      $(".update").click(function(){
        if($(this).val()=="修改"){
          $(this).val("确认");
          $(this).parent("td").prevAll("td").children(".stu").removeAttr("readonly");
        }else{
          $(this).attr("type","submit");
          $(this).val("修改");
          $(this).parent("td").prevAll("td").children(".stu").attr("readonly","readonly");
          $(this).parent("td").parent("tr").prev(".form").attr("action","/update");
        };
      });
      $(".delete").click(function () {
        if($(this).parent("td").prevAll("td").children(".update").val()=="确认"||$(this).parent("td").parent("tr").prevAll("tr").children("td").children(".update").val()=="确认"){
          alert('修改操作未完成，不可删除')
        }else {
          $(this).parent("td").parent("tr").prev(".form").attr("action","/delete");
        };
      });
    });
  </script>
</html>
